package se.wangs.observerpk;

import java.util.ArrayList;
import java.util.List;

/**
 * -- coding: UTF-8 -- *
 *
 * @author wangs
 * @date 2023/12/12 17:04
 * @description 一个具体的主题，里面包含天气的各种信息
 *              1) 实现接口标准
 *              2) 整理自己的各种信息
 */
public class WeatherData implements Subject {
    // 主题自身的信息
    private List<Observer> observerList;
    private float temperature;
    private float humidity;
    private float pressure;

    public WeatherData() {
        observerList = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observerList.remove(observer);
    }

    @Override
    public void notifyAllObservers() {
        observerList.forEach(observer -> observer.update(temperature, humidity, pressure));
    }

    public void measurementsChanged() {
        notifyAllObservers();
    }

    /**
     * 设置参数，提供测试数据
     * @param temperature   温度
     * @param humidity      湿度
     * @param pressure      气压
     */
    public void setMeasurements(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }
}
